#include "ChessAnalyse.h"
#include <algorithm>
#include "CheckWin.h"

ChessAnalyse::ChessAnalyse(const std::vector<Chess>& analyseChessVec)
    : m_analyseChessVec(analyseChessVec)
{
    std::sort(m_analyseChessVec.begin(), m_analyseChessVec.end());
}

ChessAnalyse::~ChessAnalyse()
{
    m_analyseChessVec.clear();
}

const std::vector<PrivilageChess> ChessAnalyse::checkPrivilageChess()
{
    for (size_t i = 0; i < m_analyseChessVec.size(); i++)
    {

    }

    std::vector<PrivilageChess> tmpPrivilage = checkTingChess();
    /*if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }
    std::vector<Chess> bingChessVec;
    std::vector<Chess> wanChessVec;
    std::vector<Chess> tiaoChessVec;
    std::vector<Chess> dfChessVec;
    std::vector<Chess> xfChessVec;
    std::vector<Chess> nfChessVec;
    std::vector<Chess> bfChessVec;
    std::vector<Chess> zhongChessVec;
    std::vector<Chess> faChessVec;
    std::vector<Chess> baiChessVec;

    for (size_t i = 0; i < m_analyseChessVec.size(); i++)
    {
        switch (m_analyseChessVec.at(i).type)
        {
        case BING:
            bingChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case WAN:
            wanChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case TIAO:
            tiaoChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case DF:
            dfChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case XF:
            xfChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case NF:
            nfChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case BF:
            bfChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case Zh:
            zhongChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case Fa:
            faChessVec.push_back(m_analyseChessVec.at(i));
            break;
        case Bai:
            baiChessVec.push_back(m_analyseChessVec.at(i));
            break;
        default:
            break;
        }
    }

    tmpPrivilage = checkFengChess(dfChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(xfChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(nfChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(bfChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(zhongChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(faChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }

    tmpPrivilage = checkFengChess(baiChessVec);
    if (!tmpPrivilage.empty())
    {
        return tmpPrivilage;
    }


    return tmpPrivilage;*/
    return tmpPrivilage;
}

const std::vector<PrivilageChess> ChessAnalyse::checkTingChess()
{
    std::vector<PrivilageChess> tingResult;
    for (size_t i = 0; i < m_analyseChessVec.size(); i++)
    {
        std::vector<Chess> tmpChessVec(m_analyseChessVec);
        auto iter = tmpChessVec.begin() + i;
        Chess outputChess(*iter);
        tmpChessVec.erase(iter);
        PrivilageChess privilageChess = {};
        privilageChess.outputChess = outputChess;
        for (short type = ChessType::BING; type <= ChessType::TIAO; ++type)
        {
            for (int cnt = 1; cnt <= 9; ++cnt)
            {
                Chess chess(static_cast<ChessType>(type), cnt);
                if (addChessCheckWin(tmpChessVec, chess))
                {
                    privilageChess.chessVec.push_back(chess);
                }
            }
        }

        for (short type = ChessType::DF; type <= ChessType::Bai; ++type)
        {
            Chess chess(static_cast<ChessType>(type), 0);
            if (addChessCheckWin(tmpChessVec, chess))
            {
                privilageChess.chessVec.push_back(chess);
            }
        }
        privilageChess.privilage = privilageChess.chessVec.size();
        tingResult.push_back(privilageChess);
    }
    return tingResult;
}

const std::vector<PrivilageChess> ChessAnalyse::checkFengChess(const std::vector<Chess>& fengChessVec)
{
    std::vector<PrivilageChess> tingResult;
    if (fengChessVec.size() == 1)
    {
        PrivilageChess privilageChess = {};
        privilageChess.outputChess = fengChessVec.at(0);
    }
    return tingResult;
}

const std::vector<PrivilageChess> ChessAnalyse::checkNormalChess(const std::vector<Chess>& normalChessVec)
{
    // 1 X
    if (normalChessVec.size() == 1)
    {
        return Chess1Check(normalChessVec);
    }
    // 2     
    if (normalChessVec.size() == 2)
    {
        return Chess2Check(normalChessVec);
    }

    // 3 XXX 
    if (normalChessVec.size() == 3)
    {
        return Chess3Check(normalChessVec);
    }
    // 4 1100
    if (normalChessVec.size() == 4)
    {
        return Chess4Check(normalChessVec);
    }
    // 5 1000
    if (normalChessVec.size() == 5)
    {
        return Chess5Check(normalChessVec);
    }
    return std::vector<PrivilageChess>();
}

bool ChessAnalyse::addChessCheckWin(std::vector<Chess> tingChess, const Chess& winChess)
{
    CheckWin chessCWin(tingChess);
    std::vector<KindChess> resualtVec = {};
    return chessCWin.analyseChess2Win(resualtVec);
}

bool ChessAnalyse::isMidChessCunt(short chessCunt)
{
    return ((chessCunt >= 4) || (chessCunt <= 7));
}

const std::vector<PrivilageChess> ChessAnalyse::Chess1Check(const std::vector<Chess>& normalChessVec)
{
    std::vector<PrivilageChess> tingResult;
    PrivilageChess privilageChess = {};
    privilageChess.privilage = 1400;
    privilageChess.outputChess = normalChessVec.at(0);
    tingResult.push_back(privilageChess);
    return tingResult;
}


const std::vector<PrivilageChess> ChessAnalyse::Chess2Check(const std::vector<Chess>& normalChessVec)
{


    std::vector<PrivilageChess> tingResult;
    // XX 
    if (normalChessVec.at(0).count == normalChessVec.at(1).count)
    {
        PrivilageChess privilageChess = {};
        privilageChess.privilage = 50;
        privilageChess.outputChess = normalChessVec.at(0);
        tingResult.push_back(privilageChess);
        return tingResult;
    }
    // 12-89 56 
    if (normalChessVec.at(1).count - normalChessVec.at(0).count == 1)
    {
        PrivilageChess privilageChessLow = {};
        PrivilageChess privilageChessHigh = {};
        if (normalChessVec.at(0).count == 1)
        {
            privilageChessLow.privilage = 1300;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 51;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        else if (normalChessVec.at(1).count == 9)
        {
            privilageChessLow.privilage = 51;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 1300;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        else
        {
            privilageChessLow.privilage = 49;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 49;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        tingResult.push_back(privilageChessLow);
        tingResult.push_back(privilageChessHigh);
        return tingResult;
    }

    // 13-79 57
    if (normalChessVec.at(1).count - normalChessVec.at(0).count == 2)
    {
        PrivilageChess privilageChessLow = {};
        PrivilageChess privilageChessHigh = {};
        if (normalChessVec.at(0).count == 1)
        {
            privilageChessLow.privilage = 1300;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 101;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        else if (normalChessVec.at(1).count == 9)
        {
            privilageChessLow.privilage = 101;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 1300;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        else
        {
            privilageChessLow.privilage = 52;
            privilageChessLow.outputChess = normalChessVec.at(0);
            privilageChessHigh.privilage = 52;
            privilageChessHigh.outputChess = normalChessVec.at(1);
        }
        tingResult.push_back(privilageChessLow);
        tingResult.push_back(privilageChessHigh);
        return tingResult;
    }


    //  15-59 28
    return std::vector<PrivilageChess>();
}


const std::vector<PrivilageChess> ChessAnalyse::Chess3Check(const std::vector<Chess>& normalChessVec)
{
    /*
    111 112 113 114 115 116 117 118 119 122 123 124 125 126 127 128 129 133 134 135 136 137 138 139 144 145 146 147 148 149 155 156 157 158 159 166 167 168 169 177 178 179 188 189 199
    222 223 224 225 226 227 228 229 233 234 235 236 237 238 239 244 245 246 247 248 249 255 256 257 258 259 266 267 268 269 277 278 279 288 289 299
    333 334 335 336 337 338 339 344 345 346 347 348 349 355 356 357 358 359 366 367 368 369 377 378 379 388 389 399
    444 445 446 447 448 449 455 456 457 458 459 466 467 468 469 477 478 479 488 489 499
    555 556 557 558 559 566 567 568 569 577 578 579 588 589 599
    666 667 668 669 677 678 679 688 689 699
    777 778 779 788 789 799
    888 889 899
    999
    */
    return std::vector<PrivilageChess>();
}


const std::vector<PrivilageChess> ChessAnalyse::Chess4Check(const std::vector<Chess>& normalChessVec)
{
    return std::vector<PrivilageChess>();
}


const std::vector<PrivilageChess> ChessAnalyse::Chess5Check(const std::vector<Chess>& normalChessVec)
{
    return std::vector<PrivilageChess>();
}
